unit EMSCharacter;

{$I QBuilderVerCtrl.inc}

interface

uses
  QBStringTypes;

type
  TEMSCharacter = class
  public
    class function IsAlpha(C: aqbChar): Boolean;
    class function IsDigit(C: aqbChar): Boolean;
    class function InArray(C: aqbChar; Ar: array of aqbChar): Boolean;
  end;

type
  TQBCharSet = set of AnsiChar;
function QBCharInSet(C: AnsiChar; const CharSet: TQBCharSet): Boolean; overload;
function QBCharInSet(C: WideChar; const CharSet: TQBCharSet): Boolean; overload;

implementation

{$IFDEF VCL12}
  uses
    {$IFDEF VCL16}
      System.Character,
      System.SysUtils;
    {$ELSE}
      Character,
      SysUtils;
    {$ENDIF}
{$ELSE}
  {$IFDEF EC_UNICODE}
  uses
    ecUnicode;
  {$ENDIF}
{$ELSE}
  {$IFDEF EC_UNICODE}
uses ecUnicode;
  {$ELSE}

  {$ENDIF}
{$ENDIF}

function QBCharInSet(C: AnsiChar; const CharSet: TQBCharSet): Boolean;
begin
  Result := C in CharSet;
end;

function QBCharInSet(C: WideChar; const CharSet: TQBCharSet): Boolean; 
begin
  Result := (C < #$0100) and (AnsiChar(C) in CharSet);
end;

class function TEMSCharacter.InArray(C: aqbChar; Ar: array of aqbChar): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := Low(Ar) to High(Ar) do
  begin
    if Ar[I] = C then
    begin
      Result := True;
      Break;
    end;
  end;
end;

// for Latin1 only
class function TEMSCharacter.IsAlpha(C: aqbChar): Boolean;
begin
  {$IFDEF VCL12}
    Result := TCharacter.IsLetter(C);
  {$ELSE}
//    {$IFDEF EC_UNICODE}
//      Result := UnicodeIsAlpha( UCS4(C) );
//    {$ELSE}
      Result := QBCharInSet(C, ['A'..'Z']) or QBCharInSet(C, ['a'..'z']);
//    {$ENDIF}
  {$ENDIF}
end;

class function TEMSCharacter.IsDigit(C: aqbChar): Boolean;
begin
  {$IFDEF VCL12}
  Result := TCharacter.IsDigit(C);
  {$ELSE}
//    {$IFDEF EC_UNICODE}
//      Result := UnicodeIsDigit( UCS4(C) );
//    {$ELSE}
      Result := QBCharInSet(C, ['0'..'9']);
//    {$ENDIF}
  {$ENDIF}
end;

end.
